import {Tools,Bubble,Toast} from 'weaver-mobile';
const getLabel = Tools.getLabel;

//打开流程
export const openWorkFlow = (type,requestid,callbackFun,returnUrl='') => {
    let url = '';
    if(type == 'add'){
        url = "/spa/workflow/forwardMobileForm.html?iscreate=1&workflowid="+requestid;
    }else if(type == 'view'){
        url = "/spa/workflow/forwardMobileForm.html?requestid="+requestid;
    }

    window.openLink.openWorkflow(url,callbackFun,returnUrl);
}

//打开三方
export const openSystemApp = (key) => {
    const isExist = window.em && window.em.checkJsApi('openApp');
    if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

    let app = {};
    switch (key) {
        case 'travelBooking'://商旅订票
            app.iOSscheme = 'ctrip://';
            app.iOSurl = '';
            app.params = {  };
            app.androidscheme = 'ctrip.android.view';
            app.androidurl = '';
            break;
        case 'highIronPipe'://高铁管家
            app.iOSscheme = 'gtgj://';
            app.iOSurl = '';
            app.params = { query: '' };
            app.androidscheme = 'com.gtgj.view';
            app.androidurl = '';
            break;
        case 'didiChuxing'://滴滴打车
            app.iOSscheme = 'diditaxi://dache/sendorder';
            app.iOSurl = '';
            app.params = { };
            app.androidscheme = 'OneTravel://dache/sendorder';
            app.androidurl = '';
            break;
        case 'xiecheng'://携程
            app.iOSscheme = 'CtripWireless://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'CtripWireless://';
            app.androidurl = '';
            break;  
        case 'aliNailed'://钉钉
            app.iOSscheme = 'dingtalk://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'com.alibaba.android.rimet';
            app.androidurl = '';
            break;
        case 'tcly'://同城旅游
            app.iOSscheme = 'dingtalk://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'dingtalk://';
            app.androidurl = '';
            break;            
    }
    window.em.openApp(app);
}

//扫一扫
export const _scanQRCode = (callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('scanQRCode');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

        console.log('_scanQRCode---isExist',isExist);
        window.em.scanQRCode({
            needResult: 1,
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
                console.log('res',res);
                callback(res.resultStr);
            },
            fail:function(err){
                console.log('err',err);
                Toast.fail(err);
            }
        });
    } catch (e) {
    }
};

/**
 * 控制EM客户端顶部头信息
 */
export const controlEmTopNav = (params={},callback) => {
    const isExist = window.em && window.em.checkJsApi('configNavigation');
    if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};
    
    const nav = {
        'success': function (argument) {
            console.log('controlEmTopNav success', JSON.stringify(argument));
        },
        'fail': function (argument) {
            console.log('controlEmTopNav fail', JSON.stringify(argument));
        },
        'left': {},
        'right': {  
            'name':'发票设置',
            'action':function(argument){
                callback();
            },
            
        },
        'title': '',
        showCloseButton: true
    };
   
    window.em.ready(function(){
        if(params.title===undefined){
            params.title = "";
        }
        window.em.configNavigation({...nav, ...params});
    });
}

/**
 * 重置导航栏
 */
export const resetNavBar = (params = {}) => {
    const isExist = window.em && window.em.checkJsApi('resetNavigation');
    if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

    window.em.ready(function(){
        window.em.resetNavigation({title: ''});
    });
}


/**
 * 获取 EM 主题
 */
export const getTheme = (callback) => {
    const isExist = window.em && window.em.checkJsApi('getClientInfo');
    if (!isExist) return;
    console.log('getClientInfo---isExist',isExist);
    window.em.getClientInfo({
        success:function(res){
            console.log('res',res);
            callback && callback(res);
        },
        fail:function(err){
            console.log('err',err);
            //Toast.fail(err);
            //callback && callback(err);
        }
    })
    
}

//微信发票
export const _chooseInvoice = (index,callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('chooseInvoice');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};
		console.log('_chooseInvoice---isExist',isExist);
        window.em.chooseInvoice({
            type: index,  // 类型：1 微信  2 支付宝
            success: function (res) {
				console.log('res',res);
                callback && callback(res.setInvoiceList);
            },
            fail:function(err){
				console.log('err',err);
                if(err.errMsg){
                    Bubble.warning(err.errMsg);
                }
                let _err = err.setInvoiceList ? err.setInvoiceList : err;
                callback && callback(_err);
            }
        });
    } catch (e) {
    }
    
}

//获取corpid
export const getCpUser = (callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('getCpUser');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};
        console.log('getCpUser---isExist',isExist);
        window.em.getCpUser({
            success: function (res) {
                console.log('res',res);
                callback && callback(res.cpCorpId);
            },
            fail:function(err){
                console.log('err',err);
                callback && callback(err);
                Toast.fail(err);
            }
        });
    } catch (e) {
    }
    
}
